package com.ej.busi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ej.busi.domain.EjStudentExtend;
import com.ej.busi.dto.student.EjStudentAddDto;
import com.ej.busi.mapper.EjStudentExtendMapper;
import com.ej.busi.service.IEjStudentExtendService;
import com.ej.common.core.domain.entity.SysUser;
import com.ej.common.utils.SecurityUtils;
import com.ej.system.service.ISysUserService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程表 服务层实现
 * 
 * @author ej
 */
@Service
public class EjStudentExtendServiceImpl extends ServiceImpl<EjStudentExtendMapper, EjStudentExtend> implements IEjStudentExtendService{

    @Autowired
    private ISysUserService userService;

    @Override
    @Transactional
    public void addStudent(EjStudentAddDto dto) {
        // 保存用户
        SysUser sysUser = new SysUser();
        sysUser.setUserName(dto.getPhone());
        sysUser.setNickName(dto.getStudentName());
        sysUser.setPassword(dto.getPhone().substring(5,11));
        sysUser.setRoleId(20L);
        sysUser.setDeptId(210L);
        sysUser.setCreateBy(SecurityUtils.getUsername());
        sysUser.setPassword(SecurityUtils.encryptPassword(sysUser.getPassword()));
        sysUser.setRoleIds(Lists.newArrayList(20L).toArray(new Long[]{}));
        userService.insertUser(sysUser);

        // 创建学生补充信息
        EjStudentExtend ejStudentExtend = new EjStudentExtend();
        ejStudentExtend.setUserId(sysUser.getUserId());
        ejStudentExtend.setCreateBy(SecurityUtils.getUserId());
        ejStudentExtend.setGradeId(dto.getGradeId());
        ejStudentExtend.setRemark(dto.getRemark());
        this.save(ejStudentExtend);
    }
}
